// 添加获取字幕和显示浮层的功能
async function handleSubtitleClick() {
    // 检查字幕是否存在
    if (!subtitleData) {
        try {
            // 显示加载提示
            const loadingNotice = document.createElement('div');
            loadingNotice.style.cssText = `
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                padding: 20px;
                background: rgba(0, 0, 0, 0.8);
                color: white;
                border-radius: 8px;
                z-index: 10001;
            `;
            loadingNotice.textContent = '正在加载字幕...';
            document.body.appendChild(loadingNotice);

            // 获取字幕数据
            subtitleData = await fetchSubtitles();
            
            // 移除加载提示
            loadingNotice.remove();

            if (!subtitleData || subtitleData.length === 0) {
                alert('未找到字幕文件');
                return;
            }
        } catch (error) {
            alert('获取字幕失败: ' + error.message);
            return;
        }
    }
    
    // 创建浮层
    createSubtitleOverlay(subtitleData);
}

// 创建字幕浮层
function createSubtitleOverlay(subtitles) {
    const overlay = document.createElement('div');
    overlay.className = 'subtitle-overlay';
    overlay.style.cssText = `
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 80%;
        height: 80%;
        background: rgba(34, 34, 34, 0.95);
        z-index: 10000;
        padding: 20px;
        overflow-y: auto;
        color: white;
        border-radius: 8px;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    `;

    // 添加标题
    const title = document.createElement('div');
    title.style.cssText = `
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 20px;
        padding-bottom: 10px;
        border-bottom: 1px solid #666;
    `;
    title.textContent = '视频字幕';
    overlay.appendChild(title);

    // 修改关闭按钮样式
    const closeButton = document.createElement('button');
    closeButton.textContent = '关闭';
    closeButton.style.cssText = `
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 6px 12px;
        background: #FB7299;
        border: none;
        color: white;
        border-radius: 4px;
        cursor: pointer;
        font-size: 14px;
        transition: background 0.2s;
    `;
    closeButton.onmouseover = () => {
        closeButton.style.background = '#fc8bab';
    };
    closeButton.onmouseout = () => {
        closeButton.style.background = '#FB7299';
    };
    closeButton.onclick = () => overlay.remove();
    overlay.appendChild(closeButton);

    // 修改字幕列表样式
    const subtitleList = document.createElement('div');
    subtitleList.className = 'subtitle-list';
    
    subtitles.forEach(subtitle => {
        const subtitleItem = document.createElement('div');
        subtitleItem.className = 'subtitle-item';
        subtitleItem.style.cssText = `
            padding: 12px;
            margin: 8px 0;
            cursor: pointer;
            border-radius: 4px;
            transition: all 0.2s;
            display: flex;
            align-items: center;
        `;

        // 添加时间戳
        const timeStamp = document.createElement('span');
        timeStamp.style.cssText = `
            color: #FB7299;
            margin-right: 10px;
            font-family: monospace;
            font-size: 14px;
            min-width: 80px;
        `;
        timeStamp.textContent = formatTime(subtitle.startTime);
        subtitleItem.appendChild(timeStamp);

        // 添加字幕文本
        const textSpan = document.createElement('span');
        textSpan.textContent = subtitle.text;
        textSpan.style.flex = '1';
        subtitleItem.appendChild(textSpan);
        
        // 点击和悬停效果
        subtitleItem.onclick = () => {
            const video = document.querySelector('video');
            if (video) {
                video.currentTime = subtitle.startTime;
                video.play();
                overlay.remove();
            }
        };
        
        subtitleItem.onmouseover = () => {
            subtitleItem.style.backgroundColor = '#FB729922';
        };
        subtitleItem.onmouseout = () => {
            subtitleItem.style.backgroundColor = 'transparent';
        };
        
        subtitleList.appendChild(subtitleItem);
    });
    
    overlay.appendChild(subtitleList);
    document.body.appendChild(overlay);
}

// 获取字幕数据的函数
async function fetchSubtitles() {
  // 先从本地存储中获取字幕数据
  chrome.storage.local.get(`
https://aisubtitle.hdslb.com/bfs/ai_subtitle/prod/`, (items) => {
    console.log(items);
  });
    try {
        // 获取视频aid和cid
        const aid = window.__INITIAL_STATE__?.aid;
        const cid = window.__INITIAL_STATE__?.videoData?.cid;
        
        if (!aid || !cid) {
            throw new Error('无法获取视频信息');
        }

        // 发送消息给background脚本获取字幕
        return new Promise((resolve, reject) => {
            chrome.runtime.sendMessage(
                { 
                    type: 'FETCH_SUBTITLES',
                    aid,
                    cid
                },
                response => {
                    if (response.success) {
                        resolve(response.subtitles);
                    } else {
                        reject(new Error(response.error));
                    }
                }
            );
        });
    } catch (error) {
        console.error('获取字幕失败:', error);
        throw error;
    }
} 